{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import open3d as o3d\n",
    "import torch\n",
    "from torch.utils.data import DataLoader\n",
    "\n",
    "from gradslam import RGBDImages\n",
    "from gradslam.slam import PointFusion, ICPSLAM\n",
    "from gradslam.datasets import ICL, TUM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Dataset to use\n",
    "dataset_name = \"icl\"  # \"icl\", \"tum\"\n",
    "\n",
    "# Path to the dataset directory\n",
    "dataset_path = \"G:\\Datasets\\ICL\"\n",
    "\n",
    "# Odometry method to use\n",
    "odometry = \"gt\"  # \"gt\", \"icp\", \"gradicp\"\n",
    "\n",
    "# select device\n",
    "device = torch.device(\"cuda\") if torch.cuda.is_available() else torch.device(\"cpu\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load dataset\n",
    "Dataset = ICL if dataset_name == \"icl\" else TUM\n",
    "dataset = Dataset(dataset_path, seqlen=20, height=240, width=320)\n",
    "loader = DataLoader(dataset=dataset, batch_size=2)\n",
    "colors, depths, intrinsics, poses, *_ = next(iter(loader))\n",
    "\n",
    "# create rgbdimages object\n",
    "rgbdimages = RGBDImages(colors, depths, intrinsics, poses, channels_first=False)\n",
    "\n",
    "# visualize rgbdimages\n",
    "fig1 = rgbdimages.plotly(0)\n",
    "fig1.update_layout(autosize=False, height=720, width=500).show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# SLAM\n",
    "slam = PointFusion(odom=odometry, dsratio=4, device=device)\n",
    "pointclouds, recovered_poses = slam(rgbdimages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# visualize generated pointcloud map\n",
    "# Plotly visualization\n",
    "fig2 = pointclouds.plotly(0).update_layout(autosize=False, height=600)\n",
    "fig2.show()\n",
    "\n",
    "# Open3d visualization\n",
    "# o3d.visualization.draw_geometries([pointclouds.open3d(0)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
